/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package databook.controller;

import prefuse.controls.Control;
import prefuse.controls.ControlAdapter;
import java.awt.event.*;
import java.text.*;
import prefuse.visual.*;
import prefuse.*;

/**
 *
 * @author zach
 */
public class CustomToolController extends ControlAdapter implements PropertyListener {
    VisualItem over;
    int width;
    int length;
    
    public CustomToolController(int width) {
        this.width = width;
        Props.addListener(Props.CURRENT_RANGE_MAX,this);
        Props.addListener(Props.CURRENT_RANGE_MIN,this);
        length = Props.getInt(Props.NUMBER_OF_TERMS);
    }
    
    /*
     public synchronized void itemEntered(VisualItem item,MouseEvent evt) {
        over = item;
    }
    
    public synchronized void itemExited(VisualItem item, MouseEvent evt) {
        if (item.equals(over))
            over = null;
    }/**/
    
    public synchronized void mouseMoved(MouseEvent evt) {
        ((Display)evt.getSource()).setToolTipText(getTerm(evt.getX()));
    }
    
    public synchronized void itemMoved(VisualItem item, MouseEvent evt) {
        String term = getTerm(evt.getX());
        ((Display)evt.getSource()).setToolTipText(item.getString("VALUE") + " ( " + term + ") = " + NumberFormat.getIntegerInstance().format(item.get(term)));
        
    }
    
    public String getTerm(int x) {
        double dist = x/(double)width;
        double col = Props.getInt(Props.CURRENT_RANGE_MIN) + (dist*(length));
        return DBRangeSlider.getTerm((int)Math.round(col));
    }
    
    public synchronized void propertyChanged(PropertyEvent pe) {
        calcLength();
    }
    
    public void calcLength() {
        length = Props.getInt(Props.CURRENT_RANGE_MAX) - Props.getInt(Props.CURRENT_RANGE_MIN);
    }
}
